﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.DocCodeRefBase"/>.
    /// </summary>
    public abstract class DocCodeRefBaseVM : DocCommentVM
    {
        #region /* FIELDS */

        // Should evaluate to a code object reference (Expression - SymbolicRef, Dot, Call), but can also be a string.
        protected object _codeRefVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.DocCodeRefBase"/>.
        /// </summary>
        protected DocCodeRefBaseVM(DocCodeRefBase docCodeRefBase, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(docCodeRefBase, dictionary)
        {
            object codeRef = docCodeRefBase.CodeRef;
            if (codeRef is string)
                _codeRefVM = codeRef;
            else if (codeRef is Expression)
                _codeRefVM = CreateVM((Expression)codeRef, false, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.DocCodeRefBase"/> model.
        /// </summary>
        public DocCodeRefBase DocCodeRefBase
        {
            get { return (DocCodeRefBase)CodeObject; }
        }

        /// <summary>
        /// The associated <see cref="ExpressionVM"/> or string.
        /// </summary>
        public object CodeRef
        {
            get { return _codeRefVM; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        protected override void RenderStart(CodeRenderer renderer, RenderFlags flags)
        {
            Brush brush = (DocCodeRefBase.MissingEndTag ? WARNING_BRUSH : COMMENT_TAG_BRUSH);
            if (!flags.HasFlag(RenderFlags.Description) && !UseAlternativeFormat)
            {
                renderer.RenderText("<" + DocCodeRefBase.TagName, brush, this);
                renderer.RenderText(" " + DocCodeRefBase.AttributeName + "=\"", COMMENT_TAG_BRUSH, this);
            }
            if (_codeRefVM is ExpressionVM)
                ((ExpressionVM)_codeRefVM).Render(renderer, flags | RenderFlags.ForceBorder);
            else if (_codeRefVM is string)
                renderer.RenderText((string)_codeRefVM, COMMENT_TAG_BRUSH, this);
            if (!flags.HasFlag(RenderFlags.Description))
            {
                if (!UseAlternativeFormat)
                {
                    renderer.RenderText("\"", COMMENT_TAG_BRUSH, this);
                    renderer.RenderText(_contentVM == null && !DocCodeRefBase.MissingEndTag ? "/>" : ">", brush, this);
                }
                else if (_contentVM != null)
                    renderer.RenderText(" :  ", brush, (UseProportionalFont ? TextStyle.Proportional : TextStyle.Normal), this);
            }
        }

        public override void UnRender()
        {
            if (_codeRefVM is ExpressionVM)
                ((ExpressionVM)_codeRefVM).UnRender();
            base.UnRender();
        }

        #endregion
    }
}
